<?php

namespace app\modules\clinical\controllers;

use Yii;
use app\modules\clinical\models;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\components\PtAccessControl;

/**
 * VisitController implements the CRUD actions for VisitMedicine model.
 */
class VisitController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
            'pt_access' => [
                'class' => PtAccessControl::className(),
            ]
        ];
    }

    /**
     * Lists all VisitMedicine models.
     * @return mixed
     */
    public function actionIndex()
    {
        $pt_id = Yii::$app->session->get('pt_id');
        $info = $this->findVisitInfo(Yii::$app->session->get('pt_id'));
        $dataProvider = new ActiveDataProvider([
            'query' => models\VisitMedicine::find()->where(['delete_at' => 0])->andWhere(['or', 'pt_id='.$pt_id, 'rel_id='.$pt_id]),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'info' => $info,
        ]);
    }

    /**
     * Displays a single VisitMedicine model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        $info = $this->findVisitInfo(Yii::$app->session->get('pt_id'));
        return $this->render('view', [
            'model' => $this->findModel($id),
            'info' => $info,
        ]);
    }

    /**
     * Creates a new VisitMedicine model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $post = Yii::$app->request->post();
        $model = new models\VisitMedicine();
        $info = $this->findVisitInfo(Yii::$app->session->get('pt_id'));

        if ($info->load($post) && $info->save()) {

            $model->pt_id = Yii::$app->session->get('pt_id');

            if ($model->load($post) && $model->save()) {
                return $this->redirect(['/clinical/visit/view/', 'id' => $model->pk_id]);
            }
        }

        return $this->render('create', [
            'model' => $model,
            'info' => $info,
        ]);
    }

    /**
     * Updates an existing VisitMedicine model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $info = $this->findVisitInfo(Yii::$app->session->get('pt_id'));
        $model = $this->findModel($id);

        if ($info->load(Yii::$app->request->post()) && $info->save()) {
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                return $this->redirect(['/clinical/visit/view/', 'id' => $id]);
            }
        }

        return $this->render('update', [
            'model' => $model,
            'info' => $info,
        ]);
    }

    /**
     * Deletes an existing VisitMedicine model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        $model->delete_at = strtotime(date("Y-m-d H:i:s"));
        if ($model->save()) {
            return $this->redirect('/clinical/visit');
        }

        return $this->redirect('/clinical/visit/view?id='.$id);
    }

    /**
     * Finds the VisitMedicine model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return VisitMedicine the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = models\VisitMedicine::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    protected function findVisitInfo($pt_id)
    {
        if (($model = models\VisitInfo::findOne(['pt_id' => $pt_id])) !== null) {
            return $model;
        } else {
            $model = new models\VisitInfo();
            $model->pt_history = '';
            $model->pt_body = '';
            $model->pt_solution = '';
            $model->visit_memo = '';
            $model->pt_id = $pt_id;

            return $model;
        }
    }
}
